﻿using MailKit.Net.Smtp;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MimeKit;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using SmtpClient = System.Net.Mail.SmtpClient;

namespace Y2.Controllers
{
    [Obsolete]
    public class WenJianController : Controller
    {
        private readonly IHostingEnvironment hosting;

        public WenJianController(IHostingEnvironment hosting)
        {
            this.hosting = hosting;
        }
        public IActionResult WSC([FromForm(Name = "file")] IFormFile file)
        {
            string url = HttpContext.Request.GetAbsoluteUri();
            string uniqueFileName = "";
            if (file != null)
            {
                string uploadsFolder = Path.Combine(hosting.WebRootPath, "img");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;

                string filePath = Path.Combine(uploadsFolder, uniqueFileName);

                file.CopyTo(new FileStream(filePath, FileMode.Create));
            }
            return Ok(url + uniqueFileName);
        }
        public string SendEmailMimeKit(string name,string email)
        {
            MailMessage mailMsg = new MailMessage();//实例化对象
            mailMsg.From = new MailAddress("452286329@qq.com", "陆某人");//源邮件地址和发件人
            mailMsg.To.Add(new MailAddress(email+""));//收件人地址
            mailMsg.Subject = "双塔国际有限公司";//发送邮件的标题
            StringBuilder sb = new StringBuilder();
            sb.Append(name+"恭喜你通过我公司层层筛选,欢迎进入我们这个大家庭,去看到信息第一时间电话联系,尽快来办入职手续");
            sb.Append("。双塔国际有限公司-人事部");
            mailMsg.Body = sb.ToString();//发送邮件的内容
            //指定smtp服务地址（根据发件人邮箱指定对应SMTP服务器地址）
            SmtpClient client = new SmtpClient();//格式：smtp.126.com  smtp.164.com
            client.Host = "smtp.qq.com";
            //要用587端口
            client.Port = 587;//端口
            //加密
            client.EnableSsl = true;
            //通过用户名和密码验证发件人身份
            client.Credentials = new NetworkCredential("452286329@qq.com", "jjronelpbyqabgih");
            //发送邮件
            try
            {
                client.Send(mailMsg);
            }
            catch (SmtpException ex)
            {
                return ex+"";
            }
            return "";
        }
    }
}
